<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    var html = document.getElementsByTagName("html")[0];
    console.log(html);

    loop(2000);
    function loop(delay) {
        var delayTimer = setTimeout(function () {
            var adBox = document.createElement("div");
            adBox.setAttribute("id", "adBox");
            console.log(adBox);
            html.appendChild(adBox);
            // document.documentElement.appendChild(adBox);
            adBox.style.cssText = 
            `width: 300px;
            height: 400px;
            background-color: #6ff;
            position: fixed;
            right: 5px;
            bottom: 5px;`
            
            // adBox.style.width = "300px";
            // adBox.style.height = "400px";
            // adBox.style.backgroundColor = "#6ff";
            // adBox.style.position = "fixed";
            // adBox.style.right = "5px";
            // adBox.style.bottom = "5px";

            var closeBtn = document.createElement("div");
            closeBtn.setAttribute("id", "closeBtn");
            var text = document.createTextNode("X");
            closeBtn.append(text);
            adBox.appendChild(closeBtn);
            closeBtn.style.cssText = 
            `position: absolute;
            right: 0;
            top: 0;
            background-color: red;
            color: #fff;
            padding: 2px;
            user-select: none;`

            // closeBtn.style.position = "absolute";
            // closeBtn.style.right = "0";
            // closeBtn.style.top = "0";
            // closeBtn.style.color = "#fff";
            // closeBtn.style.backgroundColor = "red";
            // closeBtn.style.userSelect = "none";

            var tips = document.createElement("div");
            tips.setAttribute("id", "tips");
            var num = 10;
            var text1 = document.createTextNode(`距离广告关闭还剩${num}秒`);
            tips.append(text1);
            adBox.append(tips);

            var timer = setInterval(function () {
                num--;
                tips.innerText = `距离广告关闭还剩${num}秒`;
                if (num == 0) {
                    clearInterval(timer);
                    adBox.style.display = "none";
                    loop(delay)
                }
            }, 1000);

            closeBtn.onclick = function () {
                adBox.style.display = "none";
                clearTimeout(delayTimer);
                clearInterval(timer);
                loop(5000);
                window.open("https://www.baidu.com");
            }

        }, delay);
    }
</script>

</html>